package com.xx.servlte;

import com.xx.entity.AuctionUser;
import com.xx.entity.VO.AuctionUserVo;
import com.xx.service.Impl.UserServiceImpl;
import com.xx.service.UserService;
import com.xx.text.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UserServlet",urlPatterns = "/UserServlet")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
        request.setCharacterEncoding("utf-8");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("action");
        switch (action) {
            case "login":
                login(request, response);
                break;
            case "regist":
                regist(request, response);
                break;
        }



    }
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        String userIsAdmin = request.getParameter("userIsAdmin");
        //调用方法
        UserService service = new UserServiceImpl();
        AuctionUser ret = service.getUser(userName);
        HttpSession session=request.getSession();
        if (ret != null) {
            if (!ret.getUserPassword().equals(userPassword) || ret.getUserIsAdmin().equals(0)) {
                request.getRequestDispatcher("AuctionServlet?action=select").forward(request,response);
                session.setAttribute("user",ret);

            } else if(!ret.getUserPassword().equals(userPassword) || ret.getUserIsAdmin().equals(1)) {
                request.getRequestDispatcher("AuctionServlet?action=selectGetAll").forward(request,response);

            }
        }
//      if(!ret.getUserPassword().equals(userPassword)){
//          System.out.println("密码不正确");
//          response.sendRedirect("Login.jsp");
//          return;
//      }
//      if (!ret.getUserIsAdmin().equals(userIsAdmin)){
//          response.sendRedirect("Administrator.jsp");
//          return;
//      }
//        response.sendRedirect("Auctions.jsp");
    }

    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //userName,userPassword,userCardNo,userTel,userAddress,userPostNumber
        request.setCharacterEncoding("utf-8");
        String userName=request.getParameter("userName");
        String userPassword=request.getParameter("userPassword");
        String userCardNo=request.getParameter("userCardNo");
        String userTel=request.getParameter("userTel");
        String userAddress=request.getParameter("userAddress");
        String userPostNumber=request.getParameter("userPostNumber");
        AuctionUserVo user=new AuctionUserVo(userName,userPassword,userCardNo,userTel,userAddress,userPostNumber);

        UserService dao=new UserServiceImpl();
        dao.add(user);
        response.sendRedirect("Login.jsp");
        //        user.setUserName(userName);
//        user.setUserPassword(userPassword);
//        user.setUserCardNo(userCardNo);
//        user.setUserTel(userTel);
//        user.setUserAddress(userAddress);
//        user.setUserPostNumber(userPostNumber);
//        if (ret>0){
//            response.sendRedirect("Login.jsp");
//        }else {
//            PrintWriter out = response.getWriter();
//            out.print("<script type='text/javascript'>history.back();</script>");
//            out.flush();
//            out.close();
//        }

    }
}
